#! /usr/bin/env python3
#开始下的mysql-connector,后来下载的mysql-connector-python就能用了
import mysql.connector
from mysql.connector import errorcode

#连接
try:
  cnx = mysql.connector.connect(user='root',
                                password='3567851xy',
                                 host='localhost',
                                database='itstar')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err) 
print(cnx)

mycursor = cnx.cursor()
 
mycursor.execute("CREATE TABLE sites (name VARCHAR(255), url VARCHAR(255))")

mycursor.execute("SHOW tables")
for x in mycursor:
  print(x)


# sql = "INSERT INTO sites (name, url) VALUES (%s, %s)"
# val = ("RUNOOB", "https://www.runoob.com")
# mycursor.execute(sql, val)

sql = "INSERT INTO sites (name, url) VALUES (%s, %s)"
val = [
  ('Google', 'https://www.google.com'),
  ('Github', 'https://www.github.com'),
  ('Taobao', 'https://www.taobao.com'),
  ('stackoverflow', 'https://www.stackoverflow.com/')
]

print(sql)
 
mycursor.executemany(sql, val)

cnx.commit()
cnx.close()
